<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

use think\facade\Env;
// 应用公共文件
//返回数据公共函数
function sendResponse($data='', $code=200, $message='')
{

    $message = $message ? $message : errorCode($code);
    return json(['data'=>$data, 'code'=>$code, 'message'=>$message]);
    
}

function errorCode($code)
{
    switch ($code) {
        case 200:
            return '请求成功';
        case 401:
            return '登录过期';
        case 601:
            return 'code获取为空';
        case 602:
            return '用户昵称不能为空';
        case 603:
            return '用户手机号不能为空';                
        case 1001:
            return '轮播图链接不能为空';
        case 1101:
            return '分类名不能为空';    
        case 1102:
            return '分类已存在';    
        case 1103:
            return '分类下有子类，不能删除，如需删除请先删除子类';
        case 1104:
            return '分类下有商品，不能删除，如需删除请先删除商品';     
        case 2001:
            return '规格名称不能为空';
        case 2002:
            return '规格名已存在';
        case 2003:
            return '此规格已存在商品中，不能删除';  
        case 3001:
            return '商品名称不能为空';
        case 3002:
            return '商品轮播图最少上传一张';
        case 4001:
            return '购物车商品数量最少为1';
        case 4002:
            return '购物车商品数量不能大于100';
        case 5001:
            return '退款原因不能为空';
        case 6001:
            return '备注信息不能为空';                                  
        default:      
            return '系统错误';
    }
}

//对uid进行加密
function think_encrypt($data, $key = '', $expire = 0) {
    $key  = md5(empty($key) ? Env::get('CRYPT_KEY') : "");
    $data = base64_encode($data);
    $x    = 0;
    $len  = strlen($data);
    $l    = strlen($key);
    $char = '';
    for ($i = 0; $i < $len; $i++) {
        if ($x == $l) $x = 0;
        $char .= substr($key, $x, 1);
        $x++;
    }
    $str = sprintf('%010d', $expire ? $expire + time():0);
    for ($i = 0; $i < $len; $i++) {
        $str .= chr(ord(substr($data, $i, 1)) + (ord(substr($char, $i, 1)))%256);
    }
    return str_replace(array('+','/','='),array('-','_',''),base64_encode($str));
}

//对uid解密
function think_decrypt($data, $key = ''){
    $key    = $key  = md5(empty($key) ? Env::get('CRYPT_KEY') : "");
    $data   = str_replace(array('-','_'),array('+','/'),$data);
    $mod4   = strlen($data) % 4;
    if ($mod4) {
       $data .= substr('====', $mod4);
    }
    $data   = base64_decode($data);
    $expire = substr($data,0,10);
    $data   = substr($data,10);
    if($expire > 0 && $expire < time()) {
        return '';
    }
    $x      = 0;
    $len    = strlen($data);
    $l      = strlen($key);
    $char   = $str = '';
    for ($i = 0; $i < $len; $i++) {
        if ($x == $l) $x = 0;
        $char .= substr($key, $x, 1);
        $x++;
    }
    for ($i = 0; $i < $len; $i++) {
        if (ord(substr($data, $i, 1))<ord(substr($char, $i, 1))) {
            $str .= chr((ord(substr($data, $i, 1)) + 256) - ord(substr($char, $i, 1)));
        }else{
            $str .= chr(ord(substr($data, $i, 1)) - ord(substr($char, $i, 1)));
        }
    }
    return base64_decode($str);
}

//封装curl_get请求
function httpGet($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_URL, $url);
    $res = curl_exec($curl);
    curl_close($curl);
    return $res;
  }


